package com.cityfood.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cityfood.mapper.CfUserRecordMapper;
import com.cityfood.pojo.CfUserRecord;
import com.cityfood.pojo.CfUserRecordExample;
import com.cityfood.pojo.CfUserRecordExample.Criteria;
import com.cityfood.service.UserRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class UserRecordServiceImpl implements UserRecordService {

	@Autowired
	private CfUserRecordMapper userRecordMapper;
	
	@Override
	public List<CfUserRecord> getRecordByUserId(Integer userId) {
		CfUserRecordExample example = new CfUserRecordExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		List<CfUserRecord> list = userRecordMapper.selectByExample(example);
		return list;
		
	}
	
	@Override
	public PageInfo<CfUserRecord> getRecordByUserIdAndPage(Integer userId, Integer currentPage) {
		//设置分页的页数和行数
		PageHelper.startPage(currentPage, 10);
		
		CfUserRecordExample example = new CfUserRecordExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		List<CfUserRecord> list = userRecordMapper.selectByExample(example);
		
		//5.获取分页信息
		PageInfo<CfUserRecord> recordListPage = new PageInfo<>(list);		
		
		return recordListPage;
	}


	@Override
	public int insertRecord(CfUserRecord record) {
		int i = userRecordMapper.insertSelective(record);
		return i;
	}

	@Override
	public CfUserRecord getRecordByUserIdAndProductId(Integer userId, Integer productId) {
		CfUserRecordExample example = new CfUserRecordExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andProductIdEqualTo(productId);
		List<CfUserRecord> list = userRecordMapper.selectByExample(example);
		if (list.size()>0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public int updateRecord(CfUserRecord record) {
		int i = userRecordMapper.updateByPrimaryKeySelective(record);
		return i;
	}

}
